/*
 * @lc app=leetcode.cn id=495 lang=typescript
 *
 * [495] 提莫攻击
 */

// @lc code=start

// 有点像超时缓存清理
function findPoisonedDuration(timeSeries: number[], duration: number): number {
    let final = timeSeries.length * duration;
    let isPoisoned = undefined as undefined | number;
    timeSeries.forEach((i) => {
        if (!isPoisoned) {
            // 无毒施加毒性
            isPoisoned = i + duration;
        } else {
            if (i > isPoisoned) {
                // 超时了，将毒清除
                isPoisoned = i + duration;
            } else {
                // 刷新计时器，需要将计数删去一部分
                final -= isPoisoned - i;
                isPoisoned = i + duration;
            }
        }
    });
    return final;
}
// @lc code=end
